﻿package src.flip{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import src.events.*;
	import src.utils.*;
	import src.caurina.transitions.*;
	public final class Page extends Sprite {
		
		private var flip:Flip = Flip.flip;
		private var config:Object = Flip.config;
		//
		public var src:String = "";
		private var loader:Loader;
		private var loaded:Boolean = false;
		private var back:Sprite;
		
		private var buffer:Buffer;
		private var main:Sprite = new Sprite();
		private var img:DisplayObject;
		
		public function Page(_src:String):void {
			src = _src;
			//
			back = new Sprite();
			back.graphics.beginFill(0xffffff, 1);
			back.graphics.drawRect(0, 0, config['page_width'], config['page_height']);
			back.graphics.endFill();
			addChild(back);
			//
			buffer = new Buffer();
			buffer.x = (config['page_width'] - buffer.width) * 0.5;
			buffer.y = (config['page_height'] - buffer.height) * 0.5;
			buffer.visible = false;
			addChild(buffer);
			addChild(main);
		}

		public function load():void {
			this.x = 0;
			this.width = config['page_width'];
			if (!src || loaded) {
				return;
			}
			loaded = true;
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			var request:URLRequest = new URLRequest(src);
            loader.load(request);
			buffer.visible = true;
		}
		
		public function set smoothing(v:Boolean):void {
			if (img) {
				if (img is Bitmap) {
					var bitmap:Bitmap = img as Bitmap;
					bitmap.smoothing = v;
				}
			}
		}
		
		public function remove():void {
			if (img) {
				if (img is Bitmap) {
					var bitmap:Bitmap = img as Bitmap;
					bitmap.bitmapData.dispose();
				}
			}
			while (main.numChildren) {
				main.removeChildAt(0);
			}
			try {
				loader.close();
			} catch (e) {
			}
			try {
				loader.unload();
			} catch (e) {
			}
			try {
				loader.unloadAndStop();
			} catch (e) {
			}
			loaded = false;
		}
		
		private function completeHandler(e:Event):void {
			buffer.visible = false;
			if (e.target.content.bitmapData) {
				var bitmap:Bitmap = new Bitmap(e.target.content.bitmapData);
				bitmap.smoothing = true;
				img = bitmap;
			} else {
				img = e.target.loader;
			}
			Flip.fit(img, config['page_width'], config['page_height']);
			main.addChild(img);
        }
		private function errorHandler(e:IOErrorEvent):void {
			loaded = false;
            buffer.visible = false;
        }
		private function progressHandler(e:ProgressEvent):void {
			buffer.per.htmlText = Math.round(e.bytesLoaded / e.bytesTotal * 100) + "%";
        }
		
	}
}